/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.common.query.expression;

/**
 * <pre>
 * 
 * join表达式
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class JoinExpression implements CompoundCriterion {

	private boolean useAlias;
	private String join;
	private String operator;

	private JoinExpression(boolean useAlias, String join, String operator) {
		this.useAlias = useAlias;
		this.join = join;
		this.operator = operator;
	}

	public static JoinExpression getLeftJoinExpression(String join) {
		return new JoinExpression(true, join, LEFTJOIN);
	}

	public static JoinExpression getLeftJoinFetchExpression(String join) {
		return new JoinExpression(true, join, LEFTJOINFETCH);
	}

	public static JoinExpression getJoinExpression(String join) {
		return new JoinExpression(true, join, JOIN);
	}

	public static JoinExpression getJoinFetchExpression(String join) {
		return new JoinExpression(true, join, JOINFETCH);
	}

	public static JoinExpression getLeftJoinInornateExpression(String join) {
		return new JoinExpression(false, join, LEFTJOIN);
	}

	public static JoinExpression getLeftJoinFetchInornateExpression(String join) {
		return new JoinExpression(false, join, LEFTJOINFETCH);
	}

	public static JoinExpression getJoinInornateExpression(String join) {
		return new JoinExpression(false, join, JOIN);
	}

	public static JoinExpression getJoinFetchInornateExpression(String join) {
		return new JoinExpression(false, join, JOINFETCH);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.common.query.expression.CompoundCriterion#getJpql(java.lang.StringBuilder
	 *      )
	 */
	@Override
	public StringBuilder getJpql(StringBuilder jpql, String alias) {
		jpql.append(operator);
		if (useAlias) {
			jpql.append(alias);
		}
		jpql.append(join);
		return jpql;
	}

}
